package org.qiaice.controller;

import lombok.RequiredArgsConstructor;
import org.qiaice.entity.Category;
import org.qiaice.entity.dto.CategoryDTO;
import org.qiaice.entity.dto.CategoryPageQueryDTO;
import org.qiaice.result.PageResult;
import org.qiaice.result.Result;
import org.qiaice.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/admin/category", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping(value = "/page")
    public Result<PageResult<Category>> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        return Result.ok(categoryService.pageQuery(
                categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize(),
                categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType()));
    }

    @PostMapping(value = "/status/{status}")
    public Result<Object> changeStatus(@PathVariable Integer status, Long id) {
        categoryService.changeStatus(status, id);
        return Result.ok(null);
    }

    @DeleteMapping
    public Result<Object> delete(Long id) {
        categoryService.removeById(id);
        return Result.ok(null);
    }

    @GetMapping(value = "/list")
    public Result<List<Category>> list(Integer type) {
        return Result.ok(categoryService.listByType(type));
    }

    @PostMapping
    public Result<Object> saveCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.saveCategory(categoryDTO.getId(), categoryDTO.getType(),
                categoryDTO.getName(), categoryDTO.getSort());
        return Result.ok(null);
    }

    @PutMapping
    public Result<Object> change(@RequestBody CategoryDTO categoryDTO) {
        categoryService.change(categoryDTO.getId(), categoryDTO.getName(), categoryDTO.getSort());
        return Result.ok(null);
    }
}
